-
-
Notifications
You must be signed in to change notification settings - Fork 778
✨ Support sqlmodel_rebuild
#1270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
sqlmodel_rebuild
Linter doesn't understand the special call handling for this case.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
|
@svlandeg tests are all green. Please review. |
|
@adsharma, are there any other use cases beside the use case with external library you've mentioned? |
|
@YuriiMotov here's how I would like this work to be viewed:
In a large repository of many thousand SQLModels, you might want to defer work. Otherwise, model loading becomes slow and expensive. Anytime you want to do multiple passes over the objects, you'll need the rebuild capability. So no, it's not something specific to my pet python library. Pydantic already implements it. But it's not lazy enough. |
|
Thanks for quick reply!
But in current implementation (this PR) initialization is not deferred, right? It just allows to re-initialize model later.
I'm afraid it's a bit too abstract. Wen need more clear reasoning to move it forward. I can imagine someone may want to dynamically add fields using decorators (e.g. for Multi-tenancy). @add_client_specific_fields(client_id)
class Hero(SQLModel, table-True):
passIs it a valid use case you have in mind for this feature? |
That's right. But this file works as a standalone library. If you prefer it to be a part of this PR, I'm happy to contribute. Re-initialize the model later has many good use cases:
Most importantly, on repos like schema-org-python such re-initialize later flow improves startup time. The repo has about 1000 models. |
yes, this is a valid use case. So are stacking other decorators: Would allow this table to be used as a node in a labelled property graph. You can further expose them via graphql or similar. |
|
Would be nice if you could summarize use cases we discussed and update the description of this PR, so that we could hide some comments as resolved. |
|
Updated the summary. Please review. |
Unfortunately, I think you misunderstood me.. |
|
In the last sentence of the description, I address "is it a promotion of a particular library?". It's certainly a promotion of some of the methods I discuss. I'm agnostic about where the code lives. Are there other compositional techniques (stacking decorators) possible? Certainly. It's really a question for Here's a more complete end to end example utilizing this approach. |
The
@sqlmodeldecorator introduced byfquery.sqlmodelmoves schema definitions and types closer to the object model (references) instead of foreign_keys.Instead of writing:
you could write:
Everything works the same as before. Only the syntax has changed. However, if
Teamalso has a foreign key toHero, then one of them will have to use forward declaration so type checkers work.In those cases we need a two pass solution where in the second pass, with the full type definition available, we can generate the correct code.
Refactor some of the existing code to introduce
sqlmodel_rebuild().Use cases and benefits
Model relationships (SQLModel objects) instead of exposing ids - an implementation detail. If someone wants to use UUID instead of int, it could be handled via meta programming instead of having to rewrite all the models.
Declarative/Flexible ID generation. Do you want each table to have a different ID space or keep them all in one global ID space where objects are guaranteed to have unique IDs regardless of the type.
Multi-model use cases - relational, columnar and graph. Each one could use its own decorator that stacks on top of
@sqlmodel.fqueryuses the labelled property graph approach (LPG). Such graphs can be further exposed viagraphqlor related technologies.Startup performance in a large repo containing 1000 sqlmodels. By deferring some of the initialization we get faster load times. The rest of the initialization could happen only when the model is instantiated.
The decorator is in a self contained python file and can be moved to a standalone library independent of
fqueryor be included as a part of this PR.